﻿/***************************************************************
* File    : Sensor.cs
* Author  : Matthew Clifton
*
* Description:
*   Includes base functions for all sensors
*
* History :
***************************************************************/

using System;
using System.Threading;
using System.Windows.Forms;

namespace RLGDashboard
{
    public abstract class Sensor
    {
        // Sensor Variables
        public UInt32 timeStamp = 0;
        public bool updated = false;
        public object[] currentSensorValues = new object[4];
        public EventWaitHandle waitHandle;

        public Sensor()
        {
            currentSensorValues[0] = 0;
            currentSensorValues[1] = 0;
            currentSensorValues[2] = 0;
            currentSensorValues[3] = 0;
        }

        //Abstract functions
        public abstract object[] convert(UInt16[] inValues);

        //Common public functions across sensors
        public UInt32 getTimeStamp()
        {
            return this.timeStamp;
        }

        public bool getUpdated()
        {
            return this.updated;
        }

        //Common private functions across sensors
        public void updateValues(BluetoothTransmission transmission)
        {
            object[] calculatedValues = convert(transmission.sensors);

            checkUpdated(calculatedValues);

            if (updated == true)
            {
                currentSensorValues = calculatedValues;
                timeStamp = transmission.timestamp;
            }
        }

        public object[] getValues()
        {
            updated = false;
            return currentSensorValues;
        }

        public void checkUpdated(object[] calculatedValues)
        {
            if ((calculatedValues[0] == currentSensorValues[0]) && (calculatedValues[1] == currentSensorValues[1]) && (calculatedValues[2] == currentSensorValues[2]) && (calculatedValues[3] == currentSensorValues[3]))
            {
                updated = false;
            }
            else
            {
                updated = true;
            }
        }
    }
}